<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>functionality test of window.performance.measure</title>
        <link rel="author" title="Intel" href="http://www.intel.com/" />
        <link rel="help" href="http://www.w3.org/TR/user-timing/#extensions-performance-interface"/>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="/webperf/tests/resources/webperftestharness.js"></script>
        <script src="/webperf/tests/resources/webperftestharnessextension.js"></script>
        <script>
        setup({ explicit_done: true });

        function onload_test()
        {
            var measures_for_timing_order = [
                ['nav2now', 'navigationStart'],
                ['loadTime', 'navigationStart', 'loadEventEnd'],
                ['loadEventEnd2a', 'loadEventEnd', 'abc'],
                ['nav2a', 'navigationStart', 'abc'],
                ['domComplete2a', 'domComplete', 'abc'],
                ['negativeValue', 1, 'navigationStart'],
            ];
            var context = new PerformanceContext(window.performance);

            mark_names.forEach(context.mark, context);
            measures_for_timing_order.forEach(context.initialMeasures, context);
            test_greater_than(context.getEntriesByName('nav2now', "measure")[0].duration, 0, 'Measure of navigationStart to now should be positive value.');
            test_greater_than(context.getEntriesByName('loadTime', "measure")[0].duration, 0, 'Measure of navigationStart to loadEventEnd should be positive value.');
            test_greater_than(0, context.getEntriesByName('negativeValue', "measure")[0].duration, 'Measure of current mark to navigationStart should be negative value.');
            test_equals(context.getEntriesByName('loadTime', "measure")[0].duration + context.getEntriesByName('loadEventEnd2a', "measure")[0].duration, context.getEntriesByName('nav2a', "measure")[0].duration, 'loadTime plus loadEventEnd to a mark "a" should equal to navigationStart to "a".');

            // Following cases test for scenarios that measure names are tied for two times
            mark_names.forEach(context.mark, context);
            measures_for_timing_order.forEach(context.initialMeasures, context);

            test_greater_than(context.getEntriesByName('nav2now', "measure")[1].duration, context.getEntriesByName('nav2now', "measure")[0].duration, 'Measure of current mark to navigationStart should be negative value.');
            test_equals(context.getEntriesByName('loadTime', "measure")[0].duration, context.getEntriesByName('loadTime', "measure")[1].duration, 'Measures of loadTime should have same duration.');
            test_greater_than(context.getEntriesByName('domComplete2a', "measure")[1].duration, context.getEntriesByName('domComplete2a', "measure")[0].duration, 'Measure from domComplete event to most recent mark "a" should have longer duration.');
            test_greater_than(context.getEntriesByName('negativeValue', "measure")[0].duration, context.getEntriesByName('negativeValue', "measure")[1].duration, 'Measure from most recent mark to navigationStart should have longer duration.');

            done();
        }
        </script>
    </head>
    <body onload="setTimeout(onload_test,0)">
        <h1>Description</h1>
        <p>This test validates functionality of the interface window.performance.measure.</p>
        <div id="log"></div>
    </body>
</html>
